package com.xxxx.seckill.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMQ配置类-Headers模式
 */
@Configuration
public class RabbitMQHeadersConfig {

    private static final String QUEUE01 = "queue_headers01";
    private static final String QUEUE02 = "queue_headers02";
    private static final String EXCHANGE = "headersExchange";

    @Bean
    public Queue headersQueue01() {
        return new Queue(QUEUE01);
    }

    @Bean
    public Queue headersQueue02() {
        return new Queue(QUEUE02);
    }

    @Bean
    public HeadersExchange headersExchange() {
        return new HeadersExchange(EXCHANGE);
    }

    @Bean
    public Binding bindingHeaders01() {
        Map<String, Object> map = new HashMap<>();
        map.put("colour", "red");
        map.put("speed", "low");
        return BindingBuilder
                .bind(headersQueue01())
                .to(headersExchange())
                .whereAny(map) // 匹配上一个即可
                .match();
    }

    @Bean
    public Binding bindingHeaders02() {
        Map<String, Object> map = new HashMap<>();
        map.put("colour", "red");
        map.put("speed", "fast");
        return BindingBuilder
                .bind(headersQueue02())
                .to(headersExchange())
                .whereAll(map) // 必须同时匹配上
                .match();
    }
}
